<template>
  <!-- :collapse-transition="false"禁止过渡效果 -->
  <el-aside :width="width">
    <el-menu
      :collapse="isCollapse"
      :collapse-transition="false"
      :default-active="route.path"
      background-color="#545c64"
      text-color="#fff"
    >
      <h3 v-show="!isCollapse">后台管理系统</h3>
      <h3 v-show="isCollapse">后台</h3>
      <el-menu-item
        v-for="item in noChildren"
        :key="item.path"
        :index="item.path"
        @click="handleMenu(item)"
      >
        <component :is="item.icon" class="icons"></component>
        <span>{{ item.label }}</span>
      </el-menu-item>
      <el-sub-menu v-for="item in hasChildren" :key="item.path" :index="item.path">
        <template #title>
          <component :is="item.icon" class="icons"></component>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item
            v-for="(subItem, subIndex) in item.children"
            :key="subItem.path"
            :index="subItem.path"
            @click="handleMenu(subItem)"
          >
            <component :is="subItem.icon" class="icons"></component>
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<script setup>
import { computed } from 'vue'
import { useAllDataStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'

const store = useAllDataStore()
const isCollapse = computed(() => store.state.isCollapse)
const menuList = computed(() => store.state.menuList)
const noChildren = computed(() => menuList.value.filter((item) => !item.children))
const hasChildren = computed(() => menuList.value.filter((item) => item.children))
const width = computed(() => (store.state.isCollapse ? '64px' : '180px'))

const router = useRouter()
const route = useRoute()
const handleMenu = (item) => {
  router.push(item.path)
  store.selectMenu(item)
}
</script>

<style lang="less" scoped>
.icons {
  width: 18px;
  height: 18px;
  margin-right: 5px;
}

.el-menu {
  border-right: none;

  h3 {
    line-height: 48px;
    color: #fff;
    text-align: center;
  }
}

.el-aside {
  height: 100%;
  background-color: #545c64;
}
</style>
